/*************************************************************************
 * This file is part of HeroesForge.
 *
 * HeroesForge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HeroesForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HeroesForge.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
package org.diskordia.heroesforge.console;

import java.awt.Frame;

import org.diskordia.heroesforge.client.ClientContext;
import org.diskordia.heroesforge.client.IClientContext;
import org.diskordia.heroesforge.client.preferences.IClientPreferences;
import org.diskordia.heroesforge.client.presentation.ICommand;
import org.diskordia.heroesforge.client.service.ServiceProxy;
import org.diskordia.heroesforge.console.commands.ShowConnectionDialogCommand;
import org.diskordia.heroesforge.core.service.IHeroesForgeService;
import org.jdesktop.application.Application;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

/**
 * Module for Guice dependency injection.
 * 
 * @author lehmamic
 */
public class InjectionModule extends AbstractModule {
	private IClientPreferences preferencesInstance;
	private Application applicationInstance;
	
	public InjectionModule(Application application, IClientPreferences preferences)
	{
		if(application == null)
		{
			throw new IllegalArgumentException("The application instance is a null reference.");
		}
		
		if(preferences == null)
		{
			throw new IllegalArgumentException("the preferences instance is a null reference.");
		}
		
		this.applicationInstance = application;
		this.preferencesInstance = preferences;
	}
	
	/* (non-Javadoc)
	 * @see com.google.inject.AbstractModule#configure()
	 */
	@Override
	protected void configure() {
		// default instances of the application skeleton
		this.bind(Application.class).toInstance(this.applicationInstance);
		this.bind(IClientPreferences.class).toInstance(this.preferencesInstance);
		
		// bind the service proxy to the inteface
		this.bind(IHeroesForgeService.class).to(ServiceProxy.class);
		
		// bind the context interface
		this.bind(IClientContext.class).to(ClientContext.class);
		
		// bind the main presenter instance for circular references
		this.bind(IMainPresenter.class).to(MainPresenter.class);
		
		// bind the commands
		this.bind(ICommand.class)
		.annotatedWith(Names.named("ConnectCommand"))
		.to(ShowConnectionDialogCommand.class);
		
		// bind the views
		this.bind(IMainView.class).to(MainFrame.class);
		this.bind(Frame.class)
		.annotatedWith(Names.named("MainView"))
		.to(MainFrame.class);
		
		this.bind(IConnectionDialogView.class).to(ConnectionDialog.class);
	}
}
